﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace VPLink2014.Data.MesApplis
{
    public static class VPLinkLogExtensionFromUsers
    {
        public static IEnumerable<string> AllDirectionsFromInternActifUsers(this VPLinkLogEntities input)
        {
            return (from user in input.Users
                    where user.Actif == true
                    where !user.Email.Contains("prestataire")
                    where !string.IsNullOrEmpty(user.Direction)
                    select user.Direction).Distinct();
        }
        public static IEnumerable<string> AllDepartementsFromInternActifUsers(this VPLinkLogEntities input)
        {
            return (from user in input.Users
                    where user.Actif == true
                    where !user.Email.Contains("prestataire")
                    select user.Departement).Distinct();
        }

        public static IEnumerable<string> AllServicesFromInternActifUsers(this VPLinkLogEntities input)
        {
            return (from user in input.Users
                    where user.Actif == true
                    where !user.Email.Contains("prestataire")
                    select user.Service).Distinct();
        }

        #region Get Direction, Departement, Service from table Users

        #region Linq Helpers
        private static IEnumerable<string> DirectionFromUsers(this VPLinkLogEntities input
            , string departementName)
        {
            return (from user in input.Users
                    where user.Actif == true
                    where !user.Email.Contains("prestataire")
                    where departementName.Equals(user.Departement, StringComparison.CurrentCultureIgnoreCase) == true
                    select user.Direction).Distinct();
        }

        private static IEnumerable<string> DirectionFromUsers(this VPLinkLogEntities input
            , string departementName
            , string serviceName)
        {
            return (from user in input.Users
                    where user.Actif == true
                    where !user.Email.Contains("prestataire")
                    where serviceName.Equals(user.Service, StringComparison.CurrentCultureIgnoreCase) == true
                    where departementName.Equals(user.Departement, StringComparison.CurrentCultureIgnoreCase) == true
                    select user.Direction).Distinct();
        }

        private static IEnumerable<string> DepartementFromUsers(this VPLinkLogEntities input
            , string serviceName)
        {
            return (from user in input.Users
                    where user.Actif == true
                    where !user.Email.Contains("prestataire")
                    where serviceName.Equals(user.Service, StringComparison.CurrentCultureIgnoreCase) == true
                    select user.Departement).Distinct();
        }
        #endregion

        public static bool DoesDirectionHaveSubDirection(this VPLinkLogEntities input, string directionName)
        {
            return input.Users.Any(u => directionName.Equals(u.Direction, StringComparison.CurrentCultureIgnoreCase)
                 && !string.IsNullOrEmpty(u.Departement));
        }

        public static List<string> DepartementNameByServiceFromUsers(this VPLinkLogEntities input
            , string seviceName)
        {
            List<string> directions = new List<string>();
            foreach (string dir in input.DepartementFromUsers(seviceName))
            {
                directions.Add(dir);
            }
            return directions;
        }

        public static List<string> DirectionNameByDepartementFromUsers(this VPLinkLogEntities input
            , string departementName)
        {
            List<string> directions = new List<string>();
            foreach (string dir in input.DirectionFromUsers(departementName))
            {
                directions.Add(dir);
            }
            return directions;
        }

        public static List<string> DirectionNameByDepartementAndServiceFromUsers(this VPLinkLogEntities input
            , string departementName
            , string serviceName)
        {
            List<string> directions = new List<string>();
            foreach (string dir in input.DirectionFromUsers(departementName, serviceName))
            {
                directions.Add(dir);
            }
            return directions;
        }


        #endregion

    }
}
